﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace TeamNitro
{
    public partial class AdminSearch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            gv1.Visible = false;
            gv2.Visible = false;
            gv3.Visible = false;

            lblTitle1.Visible = false;
            lblTitle2.Visible = false;
            lblNotFound.Visible = false;
            lblCDS.Visible = false;
            lblEle.Visible = false;

            lblTitle2.ForeColor = System.Drawing.Color.Lime;

            DataTable dt = DBManager.Search(tbxSearchID.Text);

            if (dt.Rows.Count == 0)
            {
                lblNotFound.Visible = true;
            }

            else
            {
                gv1.DataSource = dt;
                gv1.DataBind();

                String Type = dt.Columns[0].ToString();

                if (Type == "StudentID")
                {
                    lblTitle1.Text = "Student";

                    DataTable dtCDS = DBManager.SearchCDSSubject(tbxSearchID.Text);
                    DataTable dtEle = DBManager.SearchElectiveSubject(tbxSearchID.Text);

                    if (dtCDS.Rows.Count == 0 && dtEle.Rows.Count == 0)
                    {
                        lblTitle2.Text = "Student is currently not taking any subjects";
                        lblTitle2.ForeColor = System.Drawing.Color.Red;
                        lblTitle2.Visible = true;
                    }

                    else if (dtCDS.Rows.Count != 0 && dtEle.Rows.Count == 0)
                    {
                        lblTitle2.Text = "Student is currently taking the following CDS subjects";
                        lblTitle2.Visible = true;
                        gv2.DataSource = dtCDS;
                        gv2.DataBind();
                        gv2.Visible = true;
                        lblCDS.Visible = true;
                    }

                    else if (dtCDS.Rows.Count == 0 && dtEle.Rows.Count != 0)
                    {
                        lblTitle2.Text = "Student is currently taking the following Elective subjects";
                        lblTitle2.Visible = true;
                        gv3.DataSource = dtEle;
                        gv3.DataBind();
                        gv3.Visible = true;
                        lblEle.Visible = true;
                    }

                    else
                    {
                        lblTitle2.Text = "Student is currently taking the following CDS and Elective subjects";
                        lblTitle2.Visible = true;
                        gv2.DataSource = dtCDS;
                        gv2.DataBind();
                        gv2.Visible = true;
                        lblCDS.Visible = true;

                        gv3.DataSource = dtEle;
                        gv3.DataBind();
                        gv3.Visible = true;
                        lblEle.Visible = true;
                    }
                }

                else if (Type == "StaffID")
                {
                    lblTitle1.Text = "Staff";
                }

                else if (Type == "AdminID")
                {
                    lblTitle1.Text = "Admin";
                }

                else if (Type == "CDSCode")
                {
                    lblTitle1.Text = "CDS";

                    DataTable dtUser = DBManager.SearchUser("CDS", tbxSearchID.Text);

                    if (dtUser.Rows.Count == 0)
                    {
                        lblTitle2.Text = "This subject is not taken by any students";
                        lblTitle2.ForeColor = System.Drawing.Color.Red;
                        lblTitle2.Visible = true;
                    }

                    else
                    {
                        lblTitle2.Text = "This subject is currently taken by following students";
                        lblTitle2.Visible = true;
                        gv2.DataSource = dtUser;
                        gv2.DataBind();
                        gv2.Visible = true;
                    }
                }

                else
                {
                    lblTitle1.Text = "Elective";

                    DataTable dtUser = DBManager.SearchUser("Elective", tbxSearchID.Text);

                    if (dtUser.Rows.Count == 0)
                    {
                        lblTitle2.Text = "This subject is not taken by any students";
                        lblTitle2.ForeColor = System.Drawing.Color.Red;
                        lblTitle2.Visible = true;
                    }

                    else
                    {
                        lblTitle2.Text = "This subject is currently taken by following students";
                        lblTitle2.Visible = true;
                        gv2.DataSource = dtUser;
                        gv2.DataBind();
                        gv2.Visible = true;
                    }
                }

                lblTitle1.Visible = true;
                gv1.Visible = true;
            }
        }
    }
}